package com.bzyd.derby.controller;


import com.bzyd.derby.entity.User;
import com.bzyd.derby.mapper.UserMapper;
import com.bzyd.derby.plugin.Page;
import com.bzyd.derby.plugin.PageInfo;
import com.bzyd.derby.vo.PageObject;
import com.bzyd.derby.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Xianyue Dang
 * @since 2021-08-24
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/checkUserExist")
    public SysResult checkUserExist() {
        int row = userMapper.checkUserExist();
        return SysResult.ok(row);
    }

    @GetMapping("/findUserList")
    public SysResult findAllUser(String name, Integer age) {
        List<User> userList = userMapper.findUserList(name, age);
        return SysResult.ok(userList);
    }

    @GetMapping("/findByPage")
    public SysResult findAllPage(Integer pageIndex, Integer pageSize, String name) {
        if (pageIndex == null || pageSize == null || pageIndex < 1) {
            return SysResult.fail("分页参数不正确");
        }

        int rowCount = userMapper.count(name);
        if (rowCount == 0) {
            return SysResult.fail("找不到记录！");
        }
        PageObject<User> pageObject = new PageObject<>();
        pageObject.setPageCurrent(pageIndex).setPageSize(pageSize).setRowCount(rowCount);
        int startIndex = (pageIndex - 1) * pageSize;
        List<User> userList = userMapper.findByPage(name, startIndex, pageSize);
        pageObject.setPageCount((rowCount - 1) / pageSize + 1).setRecords(userList);
        return SysResult.ok(pageObject);
    }

    @GetMapping("/findByPage2")
    public SysResult findAllPage2(String name, Integer pageIndex, Integer pageSize, String sortBy, String rank) {
        try {
            Page page = new Page(pageIndex, pageSize, sortBy, rank);
            List<User> list = userMapper.findByPage2(page, name);
            PageInfo<User> pageInfo = page.getPageInfo(list);
            return SysResult.ok(pageInfo);
        } catch (Exception e) {
            return SysResult.fail(e.getMessage());
        }
    }

    @GetMapping("/findById")
    public SysResult findById(Integer id) {
        User user = userMapper.findById(id);
        return SysResult.ok(user);
    }


    @GetMapping("/saveUser")
    public SysResult saveUser(String name, Integer age, String email, BigDecimal amount, Double score) {
        User user = new User();
        user.setName(name);
        user.setAge(age);
        user.setEmail(email);
        user.setAmount(amount);
        user.setScore(score);
        user.setCreateTime(new Date());
        userMapper.saveUser(user);
        return SysResult.ok();
    }

    @GetMapping("/updateById")
    public SysResult updateById(Integer id, String name, Integer age, String email, BigDecimal amount, Double score) {
        User user = userMapper.findById(id);
        if (user == null) {
            return SysResult.fail("用户不存在！");
        }
        user.setName(name);
        user.setAge(age);
        user.setEmail(email);
        user.setAmount(amount);
        user.setScore(score);
        user.setUpdateTime(new Date());
        userMapper.updateById(user);
        return SysResult.ok();
    }

    @GetMapping("/deleteById")
    public SysResult deleteById(Integer id) {
        userMapper.deleteById(id);
        return SysResult.ok();
    }

}

